package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    // http://localhost:8080 /customer/list
    /**
     * 客户列表分页（名称模糊查询）
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @PostMapping("list")
    public Map list(Integer page, Integer rows, String  customerName){
        Map<String, Object> map = new HashMap<>();
        PageInfo pageInfo = customerService.list(page,rows,customerName);
        map.put("total",pageInfo.getTotal());
        map.put("rows",pageInfo.getList());
        return map;
    }

    //http://localhost:8080/ customer/save?customerId=1
    /**
     * 客户添加或修改
     * @param customer
     * @return
     */
    @PostMapping("save")
    public ServiceVO save(Customer customer){
        customerService.save(customer);
        return new ServiceVO(100,"请求成功");
    }

    // http://localhost:8080/customer/delete
    /**
     * 客户删除（支持批量删除）
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public ServiceVO delete(String[] ids){
        customerService.delete(ids);
        return new ServiceVO(100,"删除成功");
    }
}
